package com.ywyt.publish;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.ywyt.util.ConnUtil;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class PublishProvider {
    public static final String EXCHANGE_NAME = "publish_exchange";
    @Test
    public void publish() throws IOException, TimeoutException {
        //1.获取连接对象
        Connection connection = ConnUtil.getConnection();
        //2.获取channel
        Channel channel = connection.createChannel();
        //3.声明exchange
        /*
        * 1.exchange名称
        * 2.exchange类型 ->FANOUT 发布订阅    DIRECT 路由   TOPIC 通配方式
        * */
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
        //4.发送消息到exchange
        String msg = "发布订阅方式的消息。。。。。。。";
        channel.basicPublish(EXCHANGE_NAME,"",null,msg.getBytes());
        //5.释放资源
        channel.close();
        connection.close();
    }
}
